﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using AMS.DataAccess.Properties;

namespace AMS.DataAccess
{
    /// <summary>
    /// 保存一个BO类型中，用DBAttriutes封装的类型信息
    /// </summary>
    public class ObjectInfo
    {
        /// <summary>
        /// 保存一个BO类型中，用DBAttriutes封装的类型信息
        /// </summary>
        /// <param name="objectType"></param>
        public ObjectInfo(Type objectType)
        {
            this.ObjectType = objectType;
        }

        #region Private Fields & Properties
        /// <summary>
        /// 数据库字段信息表。
        /// 键：数据库列名
        /// 值：属性信息
        /// </summary>
        private Dictionary<string, PropertyInfo> dbFields;

        /// <summary>
        /// 主键信息表
        /// </summary>
        private List<PropertyInfo> pkFields;

        /// <summary>
        /// public 类型的成员属性，并且是可读可写的
        /// 键：属性名
        /// 值：属性信息
        /// </summary>
        private Dictionary<string, PropertyInfo> propertyMap;
        #endregion

        #region Public Properties
        /// <summary>
        /// 获取或设置用DBTableAttribute封装的表信息
        /// </summary>
        public DBTableAttribute TableInfo { get; set; }

        /// <summary>
        /// 获取数据库字段。包括了所有具有DBTableAttribute及其子类修饰的字段
        /// </summary>
        public Dictionary<string, PropertyInfo> DBFields
        {
            get
            {
                if (dbFields == null)
                {
                    dbFields = new Dictionary<string, PropertyInfo>();
                }

                return dbFields;
            }
        }

        /// <summary>
        /// 获取主键字段。这些字段已经包括在DBFields中。
        /// </summary>
        public List<PropertyInfo> PKFields
        {
            get
            {
                if (pkFields == null)
                {
                    pkFields = new List<PropertyInfo>();
                }

                return pkFields;
            }
        }

        /// <summary>
        /// 获取对象的所有pubic成员属性
        /// </summary>
        public Dictionary<string, PropertyInfo> Properties
        {
            get
            {
                if (propertyMap == null)
                {
                    propertyMap = new Dictionary<string, PropertyInfo>();
                }

                return propertyMap;
            }
        }

        /// <summary>
        /// 获取封装的类型
        /// </summary>
        public Type ObjectType
        {
            get;
            private set;
        }
        #endregion

        /// <summary>
        /// 从类型中解析出DBAttriutes封装的信息
        /// </summary>
        public void BuildObject()
        {
            BuildTableAttribute();
            BuildFields();
        }

        /// <summary>
        /// 从类型中解析出 Public 成员属性，此方法会忽略属性是否有 DBAttriutes 标注。
        /// 解析的结果可通过属性 Properties 获得
        /// </summary>
        public void BuildPlainObject()
        {
            DoBuildPlainObjectProperties();
        }
        /// <summary>
        /// 从类型中解析出 Public 成员属性，此方法会忽略属性是否有 DBAttriutes 标注。
        /// 解析的结果可通过属性 Properties 获得。
        /// 忽略字段大小写
        /// </summary>
        public void BuildPlainObjectIgnoreCase()
        {
            DoBuildPlainObjectPropertiesIgnoreCase();
        }
        #region Build Object
        /// <summary>
        /// 解析 DBTable 标注
        /// </summary>
        private void BuildTableAttribute()
        {
            object[] attributes = ObjectType.GetCustomAttributes(true);
            foreach (Attribute attr in attributes)
            {
                if (attr is DBTableAttribute)
                {
                    TableInfo = (DBTableAttribute)attr;
                    break;
                }
            }

            if (null == TableInfo)
            {
                throw new AttributeException(Resources.DBTableAttributeRequired);
            }
        }

        /// <summary>
        /// 解析列标注
        /// </summary>
        private void BuildFields()
        {
            PropertyInfo[] properties = ObjectType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo property in properties)
            {
                object[] attributes = property.GetCustomAttributes(true);
                if (attributes != null && attributes.Length > 0)
                {
                    foreach (object attr in attributes)
                    {
                        if (attr is DBPrimaryKeyColumnAttribute)
                        {
                            PKFields.Add(property);
                        }

                        if (attr is DBColumnAttribute)
                        {
                            // 在确定列名的时候，如果标注中没有指定，则使用属性名称
                            string columnName = ((DBColumnAttribute)attr).Column;
                            if (string.IsNullOrEmpty(columnName))
                            {
                                columnName = property.Name;
                            }

                            DBFields.Add(columnName, property);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 获取属性信息
        /// </summary>
        private void DoBuildPlainObjectProperties()
        {
            PropertyInfo[] properties = ObjectType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty);
            foreach (PropertyInfo property in properties)
            {
                Properties.Add(property.Name, property);
            }
        }
        private void DoBuildPlainObjectPropertiesIgnoreCase()
        {
            PropertyInfo[] properties = ObjectType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty);
            foreach (PropertyInfo property in properties)
            {
                Properties[property.Name.ToUpper()] = property;
            }
        }
        #endregion
    }
}
